package com.eshop.modules.activity.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import com.eshop.modules.aop.ForbidSubmit;
import java.io.Serializable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import cn.hutool.core.util.ObjectUtil;
import com.eshop.api.EshopException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.activity.domain.StoreBargain;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.activity.service.dto.StoreBargainQueryCriteria;
import com.eshop.modules.activity.service.StoreBargainService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "商城:砍价管理" })
@RestController
@RequestMapping({ "api" })
public class StoreBargainController
{
    private final StoreBargainService storeBargainService;
    
    public StoreBargainController(final StoreBargainService storeBargainService) {
        this.storeBargainService = storeBargainService;
    }
    
    @Log("查询砍价")
    @ApiOperation("查询砍价")
    @GetMapping({ "/yxStoreBargain" })
    @PreAuthorize("hasAnyRole('admin','YXSTOREBARGAIN_ALL','YXSTOREBARGAIN_SELECT')")
    public ResponseEntity getYxStoreBargains(final StoreBargainQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.storeBargainService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @Log("修改砍价")
    @ApiOperation("修改砍价")
    @PutMapping({ "/yxStoreBargain" })
    @PreAuthorize("hasAnyRole('admin','YXSTOREBARGAIN_ALL','YXSTOREBARGAIN_EDIT')")
    public ResponseEntity update(@Validated @RequestBody final StoreBargain resources) {
        if (resources.getBargainMinPrice().compareTo(resources.getBargainMaxPrice()) >= 0) {
            throw new EshopException("单次砍最低价不能高于单次砍最高价");
        }
        if (resources.getMinPrice().compareTo(resources.getPrice()) >= 0) {
            throw new EshopException("允许砍到最低价不能高于砍价金额");
        }
        if (ObjectUtil.isNull(resources.getId())) {
            return new ResponseEntity(this.storeBargainService.save(resources), HttpStatus.CREATED);
        }
        this.storeBargainService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除砍价")
    @ApiOperation("删除砍价")
    @DeleteMapping({ "/yxStoreBargain/{id}" })
    @PreAuthorize("hasAnyRole('admin','YXSTOREBARGAIN_ALL','YXSTOREBARGAIN_DELETE')")
    public ResponseEntity delete(@PathVariable final Integer id) {
        this.storeBargainService.removeById((Serializable)id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
